package main

import (
	"fmt"
	"github.com/kataras/iris/v12"
	_ "go-movies/model"
	"go-movies/router"
	"time"
)

func main(){
	app := iris.New()
	app.Use(Logger)
	// 设置跨域:
	app.Use(Cors)
	// 注册路由:
	router.UserRouterRegister(app)
	router.RegisterMovieRouter(app)
	app.Run(iris.Addr(":9000"))
}

func Logger(ctx iris.Context){
	path := ctx.Path()
	fmt.Println("<-" + time.Now().String() + "->   " + "Request: " + path)
	ctx.Next()
}

// 设置跨域:
func Cors(ctx iris.Context) {
	ctx.Header("Access-Control-Allow-Origin", "*")
	if ctx.Request().Method == "OPTIONS" {
		ctx.Header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,PATCH,OPTIONS")
		ctx.Header("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization")
		ctx.StatusCode(204)
		return
	}
	ctx.Next()
}